import 'package:flutter/material.dart';
import 'package:flutter_common/util/context_extension.dart';
import 'package:flutter_common/util/logger.dart';
import 'package:flutter_picker/flutter_picker.dart';

typedef DateSelectCallBack = void Function(String selectDateStr, DateTime selectDate);

enum DateType {
  YMD, // y, m, d
  YM, // y ,m
  YMD_HM, // y, m, d, hh, mm
  YMD_AP_HM, // y, m, d, ap, hh, mm
  YMDHMS, // y, m, d, hh, mm,ss
  HMS, //hh, mm,ss
}

class PickerDialog {

  static void openModalPicker(
    BuildContext context, {
    required PickerAdapter adapter,
    required String title,
    required List<int> selecteds,
    required PickerConfirmCallback clickCallBack,
    PickerSelectedCallback? onSelect,
    bool useRootNavigator = false,
  }) {
    var p = Picker(
        adapter: adapter,
        title: Text(title,
            style: TextStyle(
                color: Colors.black,
                fontSize: fontSize32,
                fontWeight: FontWeight.normal)),
        selecteds: selecteds,
        changeToFirst: true,
        cancelText: '取消',
        confirmText: '确定',
        cancelTextStyle: TextStyle(
            color: Colors.black,
            fontSize: fontSize28,
            fontWeight: FontWeight.normal),
        confirmTextStyle: TextStyle(
            color: Colors.black,
            fontSize: fontSize28,
            fontWeight: FontWeight.normal),
        itemExtent: 50,
        height: 236,
        textStyle: TextStyle(
            color: Colors.black,
            fontSize: fontSize32,
            fontWeight: FontWeight.normal),
        selectedTextStyle: TextStyle(
            color: Colors.black,
            fontSize: fontSize32,
            fontWeight: FontWeight.bold),
        onConfirm: clickCallBack,
        onSelect: onSelect,
        diameterRatio: 3,
        squeeze: 1.2,
        selectionOverlay: Container(
          margin: const EdgeInsets.only(left: 30, right: 30),
          decoration: const BoxDecoration(
              border: Border(
                  top: BorderSide(color: Color(0xFFEFEFEF)),
                  bottom: BorderSide(color: Color(0xFFEFEFEF)))),
        )); //.showModal(context);
    //处理 受理编辑-》返回键时 直接走返回拦截方法，不关闭时间弹窗问题
    showModalBottomSheet(
      context: context,
      useRootNavigator: useRootNavigator,
      builder: (context) => WillPopScope(
        child: p.makePicker(null, true),
        onWillPop: () => Future.value(true),
      ),
    );
  }

  /// 日期选择器
  static void showDatePicker(
    BuildContext context, {
    DateType? dateType,
    String? title,
    DateTime? maxValue,
    DateTime? minValue,
    DateTime? value,
    bool useRootNavigator = false,
    required DateSelectCallBack clickCallback,
  }) {
    int timeType;
    if (dateType == DateType.YM) {
      timeType = PickerDateTimeType.kYM;
    } else if (dateType == DateType.YMD_HM) {
      timeType = PickerDateTimeType.kYMDHM;
    } else if (dateType == DateType.YMD_AP_HM) {
      timeType = PickerDateTimeType.kYMD_AP_HM;
    } else if (dateType == DateType.YMDHMS) {
      timeType = PickerDateTimeType.kYMDHMS;
    } else if (dateType == DateType.HMS) {
      timeType = PickerDateTimeType.kHMS;
    } else {
      timeType = PickerDateTimeType.kYMD;
    }

    openModalPicker(context,
        useRootNavigator: useRootNavigator,
        adapter: DateTimePickerAdapter(
          type: timeType,
          isNumberMonth: true,
          yearSuffix: "年",
          monthSuffix: "月",
          daySuffix: "日",
          strAMPM: const ["上午", "下午"],
          maxValue: maxValue,
          minValue: minValue,
          value: value ?? DateTime.now(),
        ),
        title: title ?? "选择日期",
        clickCallBack: (Picker picker, List<int> selecteds) {
      DateTime time =
          (picker.adapter as DateTimePickerAdapter).value ?? DateTime.now();
      String timeStr;
      String year = time.year.toString();
      String month = twoDigits(time.month);
      String day = twoDigits(time.day);
      String hour = twoDigits(time.hour);
      String minute = twoDigits(time.minute);
      String second = twoDigits(time.second);
      if (dateType == DateType.YM) {
        timeStr = "$year年$month月";
      } else if (dateType == DateType.HMS) {
        timeStr = "$hour时$minute分$second秒";
      } else if (dateType == DateType.YMD_HM) {
        timeStr = "$year年$month月$day日$hour时$minute分";
      } else if (dateType == DateType.YMDHMS) {
        timeStr = "$year年$month月$day日$hour时$minute分$second秒";
      } else if (dateType == DateType.YMD_AP_HM) {
        var str = time.hour < 12 ? "上午" : "下午";
        timeStr = "$year年$month月$day日$str${time.hour - 12}时$minute分";
      } else {
        timeStr = "$year年$month月$day日";
      }
      //"2021-11-8";//格式不正确 无法正确转换为时间戳，"2021-11-08"这才是正确的
      clickCallback(timeStr, time);
    }, selecteds: []);
  }

  static String twoDigits(int n) {
    if (n >= 10) return "$n";
    return "0$n";
  }
}
